import requests

def send_ai_request(payload: dict) -> dict:
    """
    Sends a request to the AI service and retrieves the response.

    Args:
        payload (dict): The request payload containing the prompt and parameters.

    Returns:
        dict: The response from the AI service.
    """
    api_url = "https://api.openai.com/v1/completions"  # OpenAI's API endpoint for GPT models
    api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # Replace with your valid OpenAI API key
    # Note: You need a valid API key to use OpenAI's services.
    # To obtain an API key, follow these steps:
    # 1. Visit https://platform.openai.com/signup/ to create an account.
    # 2. After signing in, navigate to the API Keys section in your account settings.
    # 3. Generate a new API key and replace the placeholder in the code with your key.
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(api_url, json=payload, headers=headers)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error communicating with AI service: {e}")
        return {"summary": "Error retrieving summary."}

def get_book_summary(book_title: str) -> str:
    """
    Sends an AI request to retrieve a summary of the specified book.

    Args:
        book_title (str): The title of the book.

    Returns:
        str: A summary of the book.
    """
    # Construct the AI request payload
    payload = {
        "prompt": f"Provide a summary of the book titled '{book_title}'.",
        "max_tokens": 150,
        "temperature": 0.7
    }

    # Send the request to the AI service
    response = send_ai_request(payload)

    # Extract and return the summary from the response
    return response.get("choices", [{}])[0].get("text", "No summary available.")
